package org.my.hbase.test;
import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;


public class HBaseTestCase {
	//HBaseConfiguration
     static Configuration cfg = HBaseConfiguration.create();
     
     //HBaseAdmin HTableDescription
     public static void create(String table,String columnFamily) throws Exception{
    	 HBaseAdmin admin = new HBaseAdmin(cfg);
    	 if(admin.tableExists(table)){
    		 System.out.println("table exists");
    		 System.exit(0);
    	 }else{
    		 HTableDescriptor tableDescriptor = new HTableDescriptor(table);
    		 tableDescriptor.addFamily(new HColumnDescriptor(columnFamily));
    		 admin.createTable(tableDescriptor);
    		 System.out.println("create tale success");
    	 }
     }
     
     public static void put(String tablename,String row,String columnFamily,String column,String data) throws Exception{
    	 HTable table = new HTable(cfg, tablename);
    	 Put pl =new Put(Bytes.toBytes(row));
    	 pl.add(Bytes.toBytes(columnFamily),Bytes.toBytes(column),Bytes.toBytes(data));
    	 table.put(pl);
    	 System.out.println("put '"+row+"','"+columnFamily+":"+column+"','"+data+"'");
     }
     public static void get(String tablename,String row) throws Exception{
    	 HTable table = new HTable(cfg, tablename);
    	 Get get = new Get(Bytes.toBytes(row));
    	 Result result = table.get(get);
    	 System.out.println("Get:"+result);
    	 
     }
     
     public static void scan(String tablename) throws Exception{
         HTable table  = new HTable(cfg, tablename);
    	 Scan s = new Scan();
    	 ResultScanner rScanner = table.getScanner(s);
    	 for(Result r:rScanner){
    		 System.out.println("Scan:"+r);
    	 }
     }
     
     public static boolean delete(String tableName) throws Exception{
    	 HBaseAdmin admin = new HBaseAdmin(cfg);
    	 if(admin.tableExists(tableName)){
    		 try {
				admin.disableTable(tableName);
				 admin.deleteTable(tableName);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
			}
    		 
    	 }
    	 return true;
     }
     public static void main(String[] args) throws Exception {
		String tableName = "hbase_tb";
		String columnFamily = "cf";
	    
		HBaseTestCase.create(tableName, columnFamily);
		HBaseTestCase.put(tableName, "row1", columnFamily, "cl1", "666666");
		HBaseTestCase.get(tableName, "row1");
		HBaseTestCase.scan(tableName);
		//if(true==HBaseTestCase.delete(tableName)){
		//	System.out.println("Delete table success");
		//}
	}
}
